package com.yaozheng.vocationaltraining.adapter;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.adapter.event.ItemClickListener;
import com.yaozheng.vocationaltraining.fragment.LearnListFragment;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.LearnListTypeItemView;
import com.yaozheng.vocationaltraining.view.LearnListTypeItemView_;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 学习列表类型设备器
 *
 * @author Administrator
 */
public class LearnListTypeAdapter extends BaseAdapter {

    private BaseActivity baseActivity;
    private BitmapLoader bitmapLoader;

    //	private List<String> dataList;
    private JSONArray dataList;
    /**
     *
     */
    private LearnListFragment learnListFragment;

    private int selectPosition;


    public int getSelectPosition() {
        return selectPosition;
    }

    public LearnListTypeAdapter(BaseActivity baseActivity,
                                LearnListFragment learnListFragment) {
        super();
        this.baseActivity = baseActivity;
//        dataList = new ArrayList<String>();
//        dataList.add("品牌");
//        dataList.add("商品");
//        dataList.add("销售");
//        dataList.add("服务");
        this.learnListFragment = learnListFragment;
        bitmapLoader = new BitmapLoader(Glide.with(baseActivity));
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        JSONObject itemData = TypeUtils.getJsonObject(dataList, selectPosition);
        String name = TypeUtils.getJsonString(itemData, "name");
        baseActivity.setIntentInt("selectLearnIndex", selectPosition);
        baseActivity.setIntentString("learnListTitle", name);

        learnListFragment.setLearnListContent(TypeUtils.getJsonArray(itemData, "value"), name);
    }

    public JSONArray getDataList() {
        return dataList;
    }

    public void setDataList(JSONArray dataList) {
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        if (dataList == null)
            return 0;
        return TypeUtils.getJsonArraySize(dataList);
    }


    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LearnListTypeItemView learnListTypeItemView;
        if (convertView == null) {
            learnListTypeItemView = LearnListTypeItemView_.build(baseActivity);
            learnListTypeItemView.setLearnListTypeAdapter(this);
            learnListTypeItemView.setItemClickListener(learnListFragment);
        } else {
            learnListTypeItemView = (LearnListTypeItemView) convertView;
        }
        learnListTypeItemView.loadData(TypeUtils.getJsonObject(dataList, position), position, bitmapLoader);
        return learnListTypeItemView;
    }

}
